package com.wgo.precise.server.trigger;

import java.util.HashMap;
import java.util.Map;

import remato.domain.server.ejb.RequirementEjb;

import com.wgo.bpot.domain.common.CrudOperation;
import com.wgo.bpot.server.persist.PersistService;
import com.wgo.bpot.server.persist.trigger.Trigger;

public class RetrieveRequirementTrigger implements Trigger<RequirementEjb> {
	private Map<String,Long> requirementRetrievals = new HashMap<String,Long>();
	
	public void trigger(RequirementEjb triggerTarget) {
		Long retrievals = requirementRetrievals.get(triggerTarget.getName());
		if (null == retrievals) {
			retrievals = new Long(0);
		}
		++retrievals;
		requirementRetrievals.put(triggerTarget.getName(),retrievals);
	}

	public boolean isApplicable(Object persistent) {
		return (null != persistent) && (RequirementEjb.class.isInstance(persistent));
	}

	public boolean isApplicable(CrudOperation operation) {
		return CrudOperation.RETRIEVE.equals(operation);
	}

	public boolean isBefore() {
		return false;
	}
	
	public long getNoRetrievals(String requirementName){
		return (null == requirementRetrievals.get(requirementName)) ? 0 : requirementRetrievals.get(requirementName);
	}
	
	public void setPersistService(PersistService persistService) {
	}
}
